﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace WoogiUI
{
    public sealed class WoogiPlane : WoogiUIBase
    {
        private bool isRegist;
        #region 
        public void Regist()
        {
            gameObject.name = gameObject.name.Replace("(Clone)", "");
            isRegist = true;
            UIManager.Instance.RegisterPlane(gameObject.name, this);

            if (isInitComplete) return;
            isInitComplete = true;
            woogiGameObject = GetGameObject<WoogiGameObject>();
        }

        internal void OnDestroy()
        {
            UIManager.Instance.DestroyPlane(gameObject.name, true);
        }
        #endregion
        private Dictionary<string, WoogiGameObject> woogiGameObject = new Dictionary<string, WoogiGameObject>();
        private bool isInitComplete = false;
        public void Awake()
        {
            if (!isRegist)
                Regist();
        }

        public GameObject GetGameObject(string objectname)
        {
            if (Find<WoogiGameObject>(woogiGameObject, objectname) != null)
            {
                return woogiGameObject[objectname].gameObject;
            }
            else
            {
                Transform tf = transform.Find(objectname);
                return tf != null ? tf.gameObject : null;
            }
        }
    }
}


